package com.lp.component_master;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentContainerView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.lp.module_lib.api.BasePresenter;
import com.lp.module_lib.base.BaseARouter;
import com.lp.module_lib.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Main2Activity extends BaseActivity {

    @BindView(R.id.nav_host_container)
    FragmentContainerView mNavHostContainer;
    @BindView(R.id.bottom_navigation_view)
    BottomNavigationView mBottomNavigationView;

    private Fragment[] mFragments;

    private int index = 0, currentTabIndex = -1;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.app_activity2_main;
    }

    @Override
    protected void initData() {

        Fragment HomeFragment = (Fragment) ARouter.getInstance().build(BaseARouter.RouterHome).navigation();
        Fragment VideoFragment = (Fragment) ARouter.getInstance().build(BaseARouter.RouterJhxw).navigation();
        Fragment NewsFragment = (Fragment) ARouter.getInstance().build(BaseARouter.RouterNews).navigation();
        Fragment MineFragment = (Fragment) ARouter.getInstance().build(BaseARouter.RouterHome).navigation();

        mFragments = new Fragment[]{
                HomeFragment,
                VideoFragment,
                NewsFragment,
                MineFragment};

        mBottomNavigationView.setItemIconTintList(null);
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home_a:
                        index = 0;
                        onTextClicked();
                        return true;
                    case R.id.navigation_home_b:
                        index = 1;
                        onTextClicked();
                        return true;
                    case R.id.navigation_home_c:
                        index = 2;
                        onTextClicked();
                        return true;
                    case R.id.navigation_home_d:
                        index = 3;
                        onTextClicked();
                        return true;
                }
                return false;
            }
        });
        mBottomNavigationView.setSelectedItemId(mBottomNavigationView.getMenu().getItem(0).getItemId());
    }

    private void onTextClicked() {
        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
        if (currentTabIndex != index) {
            if (currentTabIndex != -1) {
                trx.hide(mFragments[currentTabIndex]);
            }

            if (!mFragments[index].isAdded()) {
                trx.add(R.id.nav_host_container, mFragments[index]);
            }
            trx.show(mFragments[index]).commit();
            currentTabIndex = index;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}